package com.hhxy.rabbitmq.demo06;

import com.hhxy.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author ghp
 * @date 2023/2/24
 * @title 消费者
 * @description
 */
public class ReceiveLogsDirect02 {

    // 队列名
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtil.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明一个队列
        channel.queueDeclare("disk", false, false, false, null);
        // 将交换机和队列进行绑定
        channel.queueBind("disk", EXCHANGE_NAME, "error");

        System.out.println("ReceiveLogsDirect02等待接收消息...");
        // 接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("消息接收成功: "+new String(message.getBody()));
            System.out.println("交换机: " + message.getEnvelope().getExchange());
            System.out.println("路由键: " + message.getEnvelope().getRoutingKey());
        };
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息接收失败:" + consumerTag);
        };
        channel.basicConsume("disk", true, deliverCallback, cancelCallback);
    }
}
